package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;

import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author yjj
 * @date 2021年09月20日 17:30
 */
@Api("上传接口")
@RestController
@RequestMapping("/api/hosp")
@CrossOrigin
public class ApiController {
    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    //上传医院接口
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request){
        //获取传递过来的数据
        Map<String, String[]> requestMap = request.getParameterMap();
        //将字符串数组转换成Object类型
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(requestMap);

        //1、获取医院系统传递过来的签名,进行了MD5的加密
        String sign = (String) switchMap.get("sign");
        //2、根据传递过来医院编码，查询数据库，查询签名
        String hoscode = (String) switchMap.get("hoscode");
        String signkey = hospitalSetService.getSignKey(hoscode);
        //3、把数据库查出来的key用MD5加密、
        String encrypt = MD5.encrypt(signkey);
        //4、判断签名是否一致
        if (!sign.equals(encrypt)){
            throw  new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //传输过程中“+”转换成了“”，因此我们要转换回来
        String logoData = (String)switchMap.get("logoData");
        if (!StringUtils.isEmpty(logoData)){
            logoData = logoData.replaceAll(" ","+");
            switchMap.put("logoData",logoData);
        }
        //调用service方法
        hospitalService.save(switchMap);
        return Result.ok();
    }

    //查询医院接口
    @PostMapping("/hospital/show")
    public Result show(HttpServletRequest request){
        //获取传递过来的数据
        Map<String, String[]> requestMap = request.getParameterMap();
        //将字符串数组转换成Object类型
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(requestMap);
        //根据传递过来医院编码，查询数据库，查询签名
        String hoscode = (String) switchMap.get("hoscode");
        //1、获取医院系统传递过来的签名,进行了MD5的加密
        String sign = (String) switchMap.get("sign");
        //2、根据传递过来医院编码，查询数据库，查询签名
        String signkey = hospitalSetService.getSignKey(hoscode);
        //3、把数据库查出来的key用MD5加密、
        String encrypt = MD5.encrypt(signkey);
        //4、判断签名是否一致
        if (!sign.equals(encrypt)){
            throw  new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //传输过程中“+”转换成了“”，因此我们要转换回来
        String logoData = (String)switchMap.get("logoData");
        if(!StringUtils.isEmpty(logoData)){
            logoData = logoData.replaceAll(" ","+");
            switchMap.put("logoData",logoData);
        }
        //调用service方法实现查询
        Hospital hospital = hospitalService.getByHospital(hoscode);
        return Result.ok(hospital);
    }

    /**
     * 删除科室接口
     * @author yjj
     * @date 2021/9/21 16:32
     * @param request
     * @return com.atguigu.yygh.common.result.Result
     */
    @PostMapping("/department/remove")
    public Result remoceDepartment(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
//必填
        String depcode = (String)paramMap.get("depcode");
        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
//签名校验
       // if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
       //     throw new YyghException(ResultCodeEnum.SIGN_ERROR);
       // }

        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }
    /**
     * 查询科室
     * @author yjj
     * @date 2021/9/21 16:03
     */
    @PostMapping("/department/list")
    public Result getDepartmentList(HttpServletRequest request){
        //获取传递过来的数据
        Map<String, String[]> requestMap = request.getParameterMap();
        //将字符串数组转换成Object类型
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(requestMap);
        //根据传递过来医院编码，查询数据库，查询签名
        String hoscode = (String) switchMap.get("hoscode");
        int page = StringUtils.isEmpty(switchMap.get("page")) ? 1 : Integer.parseInt((String)switchMap.get("page"));
        int limit = StringUtils.isEmpty(switchMap.get("limit")) ? 10 : Integer.parseInt((String)switchMap.get("limit"));
        //1、获取医院系统传递过来的签名,进行了MD5的加密
        String sign = (String) switchMap.get("sign");
        //2、根据传递过来医院编码，查询数据库，查询签名
        String signkey = hospitalSetService.getSignKey(hoscode);
        //3、把数据库查出来的key用MD5加密、
        String encrypt = MD5.encrypt(signkey);
        //4、判断签名是否一致
    //    if (!sign.equals(encrypt)){
    //        throw  new YyghException(ResultCodeEnum.SIGN_ERROR);
    //    }
        //调用service方法实现查询
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> pageModel = departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);
    }
    /**
     * 上传科室
     * @author yjj
     * @date 2021/9/21 15:40
     * @param request
     * @return com.atguigu.yygh.common.result.Result
     */
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        //将string【】转换成Object
        Map<String, Object> switchMap = HttpRequestHelper.switchMap(parameterMap);
        //1、获取医院系统传递过来的签名,进行了MD5的加密
        String sign = (String) switchMap.get("sign");
        //2、根据传递过来医院编码，查询数据库，查询签名
        String hoscode = (String) switchMap.get("hoscode");
        String signkey = hospitalSetService.getSignKey(hoscode);
        //3、把数据库查出来的key用MD5加密、
        String encrypt = MD5.encrypt(signkey);
        //4、判断签名是否一致
        if (!sign.equals(encrypt)){
            throw  new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //传输过程中“+”转换成了“”，因此我们要转换回来
        String logoData = (String)switchMap.get("logoData");
        if (!StringUtils.isEmpty(logoData)){
            logoData = logoData.replaceAll(" ","+");
            switchMap.put("logoData",logoData);
        }

        departmentService.saveDepartment(switchMap);

        return Result.ok();
    }

    @ApiOperation(value = "上传排班")
    @PostMapping("/saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
//签名校验
      //  if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
      //      throw new YyghException(ResultCodeEnum.SIGN_ERROR);
      //  }

        scheduleService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("/schedule/list")
    public Result schedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
//非必填
        String depcode = (String)paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
//签名校验
        if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pageModel = scheduleService.selectPage(page , limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
//必填
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
//签名校验
     //   if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
     //       throw new YyghException(ResultCodeEnum.SIGN_ERROR);
     //   }

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

    //根据医院编号 和 科室编号 ，查询排班规则数据
    @ApiOperation(value ="查询排班规则数据")
    @GetMapping("getScheduleRule/{page}/{limit}/{hoscode}/{depcode}")
    public Result getScheduleRule(@PathVariable long page,
                                  @PathVariable long limit,
                                  @PathVariable String hoscode,
                                  @PathVariable String depcode) {
        Map<String,Object> map
                = scheduleService.getRuleSchedule(page,limit,hoscode,depcode);
        return Result.ok(map);
    }

    //根据医院编号 、科室编号和工作日期，查询排班详细信息
    @ApiOperation(value = "查询排班详细信息")
    @GetMapping("getScheduleDetail/{hoscode}/{depcode}/{workDate}")
    public Result getScheduleDetail( @PathVariable String hoscode,
                                     @PathVariable String depcode,
                                     @PathVariable String workDate) {
        List<Schedule> list = scheduleService.getDetailSchedule(hoscode,depcode,workDate);
        return Result.ok(list);
    }
}
